//
//  LKStatusPictureView.swift
//  LKWeibo
//
//  Created by like on 16/8/14.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

// cell 可重用标示符
private let HMStatusPictureViewCellId = "HMStatusPictureViewCellId"

// cell 间距 和 宽高
private let margin: CGFloat = 5
private let cellWH: CGFloat = 80
class LKStatusPictureView: UICollectionView {
    //供外界调用
    var pictureInfos: [LKPictureInfo]?{
        didSet{
            
            contentLabel.text = "\(pictureInfos?.count ?? 0)"
            
            let size = getPictureViewSize(pictureInfos?.count ?? 0)
            //更新约束   有可能cell复用  之前就存在约束了  所以要用update
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            
            //刷新数据
            reloadData()
        }
        
    }
    
    
    
    //计算配图的大小 size
    func getPictureViewSize(count:Int) -> CGSize{
        
        // 行数和列数
        let col = count == 4 ? 2 : count > 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
        
        // 计算宽和高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*margin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*margin
        
        return CGSize(width: w , height: h)
        
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置视图
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(contentLabel)
        
        contentLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        dataSource = self
        registerClass(LKStatusPictureViewCell.self, forCellWithReuseIdentifier: HMStatusPictureViewCellId)
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        
    }
    
    //懒加载一个lab
    private var contentLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: 30)
    
}

extension LKStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMStatusPictureViewCellId, forIndexPath: indexPath) as! LKStatusPictureViewCell
        
        cell.picUrl = pictureInfos![indexPath.item]
        
        return cell
    }
    
    
}


// 自定义的cell   直接在这里创建个类

class LKStatusPictureViewCell: UICollectionViewCell {
    
    var picUrl: LKPictureInfo?{
        didSet{
            bgImageView.lk_setImageWithString(picUrl?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            //判断图片是否是gif
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif"){
                // 显示(cell 复用问题)
                gifImageView.hidden = false
            }else {
                // 隐藏(cell 复用问题)
                gifImageView.hidden = true
                
            }
  
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor.clearColor()
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        // 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) in
            make.bottom.trailing.equalTo(contentView)
        }
    }
    
    
    
//背景图片
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imgName: "weibo_placeholder")
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    
    
    //gif图片
    private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")
}


